package com.hopoke.user.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopoke.common.expection.BadRequestException;
import com.hopoke.model.domain.dto.user.LoginDTO;
import com.hopoke.model.domain.po.user.User;
import com.hopoke.model.domain.vo.user.LoginVO;
import com.hopoke.model.domain.vo.user.ValidateCodeVO;
import com.hopoke.user.mapper.UserMapper;
import com.hopoke.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
* @author zengjing
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-12-05 23:24:10
*/
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    private final UserMapper userMapper;

    private final RedisTemplate<String,String> redisTemplate;

    public User getById(Long id) {
        return userMapper.getUserRoleById(id);
    }

    public LoginVO login(LoginDTO loginDTO) {

        String captcha = loginDTO.getCaptcha();
        String codeKey = loginDTO.getCodeKey();
        if (captcha == null || !captcha.equalsIgnoreCase(redisTemplate.opsForValue().get("user:validate" + codeKey))) {
            throw new BadRequestException("验证码不正确");
        }

        redisTemplate.delete("user:validate" + codeKey);

        User user = lambdaQuery().eq(User::getUserName, loginDTO.getUserName()).one();
        if (user == null) {
            throw new BadRequestException("用户名不存在");
        }

        String inputPsw = loginDTO.getUserPsw();
        String dbPws = user.getUserPsw();

        String hexInputPsw = DigestUtils.md5DigestAsHex(inputPsw.getBytes());
        if (!dbPws.equals(hexInputPsw)) {
            throw new BadRequestException("密码不正确");
        }

        String token = UUID.randomUUID().toString().replaceAll("-","");

        redisTemplate.opsForValue()
                .set("user:login" + token,
                JSON.toJSONString(user),
                7, TimeUnit.DAYS);

        LoginVO loginVO = new LoginVO();
        loginVO.setToken(token);
//        BeanUtil.copyProperties(user, loginVO);
        return loginVO;
    }

    @Override
    public ValidateCodeVO generateValidateCode() {
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(150, 40, 4, 3);
        String key = UUID.randomUUID().toString().replaceAll("-", "");

        redisTemplate.opsForValue().set("user:validate" + key, captcha.getCode(), 1, TimeUnit.MINUTES);

        ValidateCodeVO validateCodeVO = new ValidateCodeVO();
        validateCodeVO.setCodeKey(key);
        validateCodeVO.setCaptcha(captcha.getCode());
        validateCodeVO.setImage("data:image/png;base64," + captcha.getImageBase64());
        return validateCodeVO;
    }

    @Override
    public User getUserInfo(String token) {
        String s = redisTemplate.opsForValue().get("user:login" + token);
        return JSON.parseObject(s, User.class);
    }

    @Override
    public void logout(String token) {
        redisTemplate.delete("user:login" + token);
    }
}




